define(['./config'], () => {
    class Cart {
        constructor() {
            this.bottom = document.querySelector('.cart-bottom')
            this.data = JSON.parse(localStorage.getItem("carts"))
            this.li = document.querySelector('.cart-content')
            this.content = document.querySelector('.cart-item')
            this.money = document.querySelector('.resmoney>b')
            this.selectAll = document.querySelector('.checkdeall')
            this.totalNum = document.querySelector('.zongshu')
            this.del = document.querySelector('.icon-shanchu')
            this.select=document.querySelector('.select')
            this.inint()
        }
        inint() {

            this.resert()
            this.bodyClick()
            this.congst()
        }
        resert() {
            this.content.innerHTML = ''
            if (this.data == null) {
                this.content.innerHTML += `
            <h1 class="myh1">您的购物没有游戏哦! 快去添加吧!<a href="http://127.0.0.1:2113/html/list.html">Go</a></h1>
            `
                console.log(this.data);
            } else {

                this.data.forEach(produce => {
                    this.content.innerHTML += `
                <div class="cart-content">
                <div class="check">
                    <input type="checkbox" data-des=${produce.gameId} value="" ${produce.is_select && 'checked'} class="select" >
                </div>
                <div class="item">
                    <img src="${produce.gameIcon}" alt="">
                    <div class="titledes">
                        <span>${produce.gameName}</span>
                        <p class="myppp">${produce.gameDesc}</p>
                        
                    </div>
                    <b>￥${produce.price}</b>
                </div>
                <div class="del" data-des=${produce.gameId} >
                    <span class="iconfont icon-shanchu"></span>
                </div>
            </div>
                `
                })
            }
        }
        bodyClick() {
            //全选
            this.bottom.addEventListener('click', e => {
                e = e || window.event
                let target = e.target || e.srcElement
                if (target.className == "checkdeall") {
                    let type = target.checked
                    this.data.forEach(sltype => {
                        sltype.is_select = type
                    })
                    this.resert()
                    this.congst()
                }
                //删除
                if (target.className == 'del') {
                    let idd = target.dataset.des - 0
                    let index = this.data.findIndex(produce => {
                        return produce.gameId == idd
                    })

                    this.data.splice(index, 1)

                    this.resert()
                    this.congst()

                }
                if(target.className=="select"){
                    let idd=target.dataset.des-0
                    let produce=this.data.find(product=>{return product.gameId==idd})
                   console.log(produce)
                    produce.is_select=!produce.is_select
                    this.resert()
                    this.congst()

                }
                // if(this.select.){
                //     let money=0
                //     this.data.forEach(item=>{
                //         money+=item.price
                //     })
                //     console.log(money);
                    
                //     this.resert()
                //     this.congst()

                // }
            }, false)

        }
        congst() {
            let num = 0
            let moneys = 0
            this.data.forEach(money => {

                if (money.is_select == true) {
                    num += money.number
                    moneys += money.number * money.price

                    let res = this.data.every(item => {
                        return item.is_select == true
                    })
                    if (res) {
                        this.selectAll.checked = true
                    } else {
                        this.selectAll.checked = false
                    }
                }
            })
            this.money.innerHTML = moneys
            this.totalNum.innerHTML = num

        }
    }
    return Cart
})